﻿/*************************************************************************
* @File			: LuaBehaviour.cs
* @Description	: xlua脚本绑定
*
*
*
*
************************************************************************/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using XLua;

namespace Framework
{

    [System.Serializable]
    public class Injection
    {
        public string name;
        public GameObject value;
    }

    [LuaCallCSharp]
    public class LuaBehaviour : MonoBehaviour
    {
        // @brief 指定那个Lua文件
        public string luaFile;
        // @brief 绑定到lua
        public Injection[] injections;
        // @brief lua环境
        internal static LuaEnv luaEnv = null;


        // @brief 调用lua函数
        private LuaFunction luaAwake;
        // @brief 调用lua代码的开始函数
        private LuaFunction luaStart;
        // @brief 调用lua代码的物理更新函数
        private LuaFunction luaFixedUpdate;
        // @brief 调用lua代码的更新函数
        private LuaFunction luaUpdate;
        // @brief 调用lua代码的更新之后函数
        private LuaFunction luaUpdateLate;
        // @brief 调用lua代码的销毁函数
        private LuaFunction luaOnDestroy;
        // @brief 传给lua代码的数据
        private LuaTable scriptEnv;


         


        // -----------------------------------------------------------------------
        // @brief 唤醒
        void Awake()
        {
            // 获取环境
            luaEnv = LuaManager.Instance.GetEnv();

            // 加载lua代码
            object[] kRet = LuaManager.Instance.DoString($"return require('{luaFile}').new()", luaFile);
            if (null == kRet)
            {
                Logger.LogWarn("Luafile need return local table");
            }

            // 转换成Lua表数据
            scriptEnv = kRet[0] as LuaTable;

            // 赋值给lua
            foreach (var injection in injections)
            {
                scriptEnv.Set(injection.name, injection.value);
            }

            // 设置luaCom
            scriptEnv.Set("luaCom", this);

            // 设置gameObject
            scriptEnv.Set("gameObject", gameObject);



                




            scriptEnv.Get("Awake", out luaAwake);
            scriptEnv.Get("Start", out luaStart);
            scriptEnv.Get("FixedUpdate", out luaFixedUpdate);
            scriptEnv.Get("Update", out luaUpdate);
            scriptEnv.Get("UpdateLate", out luaUpdateLate);
            scriptEnv.Get("OnDestroy", out luaOnDestroy);


            if (luaAwake != null)
            {
                luaAwake.Call(scriptEnv);
            }

        }
        // -----------------------------------------------------------------------
        // @brief 开始
        void Start()
        {
            if (luaStart != null)
            {
                luaStart.Call(scriptEnv);
            }
        }

        // -----------------------------------------------------------------------
        // @brief 物理更新
        void FixedUpdate()
        {
            if (luaFixedUpdate != null)
                luaFixedUpdate.Call(scriptEnv);
        }

        // -----------------------------------------------------------------------
        // @brief 更新
        void Update()
        {
            if (luaUpdate != null)
            {
                luaUpdate.Call(scriptEnv);
            }
        }

        // -----------------------------------------------------------------------
        // @brief 更新之后
        void UpdateLate()
        {
            if (luaUpdateLate != null)
            {
                luaUpdateLate.Call(scriptEnv);
            }
        }
        // -----------------------------------------------------------------------
        // @brief 销毁
        void OnDestroy()
        {
            if (luaOnDestroy != null)
            {
                luaOnDestroy.Call(scriptEnv);
            }
            luaOnDestroy = null;
            luaUpdate = null;
            luaStart = null;
            injections = null;
            scriptEnv.Dispose();
            scriptEnv = null;
            luaEnv = null;
        }
    }
}
